/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.web.backing;

/**
 *
 * @author milos
 */
import com.factory.web.service.FactoryServiceInterface;
import com.factory.web.service.problems.ProblemEnum;
import com.factory.web.validation.InputValidation;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("fileUploadBackingBean")
@Scope("request")
public class FileUploadBackingBean {

    private boolean runPossible = false;
    private boolean setLevensteinParameters = false;
    private InputStream inputStream;
    private UploadedFile file;
    @Autowired
    FactoryServiceInterface factoryService;
    private final static int MAX_NUM_FILES = 1;
    private int counter = 0;

    public void handleFileUpload(FileUploadEvent event) throws IOException {

        if (counter >= MAX_NUM_FILES) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Choose one File.", "More than one file chosen");
            FacesContext.getCurrentInstance().addMessage("aaa", msg);
        } else {

            InputValidation inputValidation = new InputValidation();

            InputStream inputStreamTmp = null;
            inputStream = null;

            try {
                inputStreamTmp = event.getFile().getInputstream();
                inputStream = event.getFile().getInputstream();
                counter++;
            } catch (IOException ex) {
                Logger.getLogger(FileUploadBackingBean.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (!inputValidation.validateFileInput(inputStreamTmp, factoryService.getProblem().getAlgorithmicProblem())) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "File validation error.", "Wrong input.");
                FacesContext.getCurrentInstance().addMessage("aaa", msg);
                runPossible = false;
                FacesContext.getCurrentInstance().getExternalContext().redirect("fileUpload.xhtml");
            } else {
                FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
                runPossible = true;
                factoryService.upload(this.inputStream);
                runPossible = false; //because of "session" context
                FacesContext.getCurrentInstance().getExternalContext().redirect("schedulerResult.xhtml");
            }
        }
    }

    public void runScheduler() throws IOException {

        factoryService.upload(this.inputStream);
        runPossible = false; //because of "session" context
        FacesContext.getCurrentInstance().getExternalContext().redirect("schedulerResult.xhtml");
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public boolean isRunPossible() {
        return runPossible;
    }

    public void setRunPossible(boolean runPossible) {
        this.runPossible = runPossible;
    }

    public boolean isSetLevensteinParameters() {
        return setLevensteinParameters;
    }

    public void setSetLevensteinParameters(boolean setLevensteinParameters) {
        this.setLevensteinParameters = setLevensteinParameters;
    }

    public void enableLevensteinDataUpload() {
        this.setLevensteinParameters = true;
        RequestContext requestContext = RequestContext.getCurrentInstance();
        requestContext.update("uploadForm:fileUpload");
        requestContext.update("parameterForm:panel");
    }
}
